package com.yc.common.secure.support;

import com.yc.common.secure.domain.SecureUserDetailsServiceImpl;
import com.yc.service.UserService;
import com.yc.service.impl.StudentInfoServiceImpl;
import com.yc.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Objects;

/**
 * @program: model_project
 * @description:
 * @author: 作者 huchaojie
 * @create: 2023-06-11 16:29
 */
@Component
public class SecureAuthenticationProvider implements AuthenticationProvider {

    @Resource
    private UserService userService;
    @Resource
    private SecureUserDetailsServiceImpl userDetailsService;
    @Resource
    private StudentInfoServiceImpl studentInfoService;
    /**
     * false 代表教师
     * true 代表学生
     */
    public static boolean status =false;
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String identity = request.getParameter("identity");
        String checkValue = request.getParameter("checkValue");
        UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
        String principal =(String) token.getPrincipal();
        UserDetails userDetails =null;

        if (checkValue.equals("false")){
            int login = studentInfoService.login(principal, token.getCredentials().toString(), 0, request);
            if (login==0){
                throw new UsernameNotFoundException("用户名密码不正确！");
            }else {
                status=true;
                userDetails = userDetailsService.loadUserByUsername(token.getPrincipal().toString());
            }
        }else {
            if (principal.length()==11){
                studentInfoService.login(principal,token.getCredentials().toString(),0,request);
                userDetails = userDetailsService.loadUserByUsername(token.getPrincipal().toString());
            }else if (principal.length()==10||principal.equals("admin")||principal.matches("[a-zA-Z0-9]+")){
                status=false;
                userService.login(token.getName(), token.getCredentials().toString(), identity, request);
                userDetails = userDetailsService.loadUserByUsername(token.getName());
            }else {
                throw new UsernameNotFoundException("用户名不符合规范");
            }
        }
        return new UsernamePasswordAuthenticationToken(userDetails, token.getCredentials().toString(),userDetails.getAuthorities());
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }

}
